home *** CD-ROM | disk | FTP | other *** search
- <SCRIPT RUNAT=SERVER LANGUAGE=VBSCRIPT>
-
- '////////////////////////////////////////////////////////////////////
- '// //
- '// NetObjects Fusion ASP Components //
- '// Custom JScript Library Functions //
- '// //
- '////////////////////////////////////////////////////////////////////
- '/*
- 'Version: 1.0 9/23/97
- '
- 'Written By: Application Methods, Inc.
- ' 6300 Southcenter Blvd.
- ' Seattle, WA 98188
- ' (206) 244-2400
- ' http://www.appmethods.com
- '
- 'DESCRIPTION: This file contains a variety of functions common to various
- 'Fusion database components.
- '
- '=====================================================================*/
-
- '
- ' Formats a date object into a string of the form mm-dd-yyyy
- '
- function formatDate( MyDate )
-
- DATEDELIM = "-"
- returnVal = ""
-
- if ((not isnull(myDate)) and (myDate <> "")) then
- ' Put in format mm-dd-yyyy
-
- if len(Month(myDate)) = 1 then
- monthPad = "0"
- else
- monthPad = ""
- end if
- if len(Day(myDate)) = 1 then
- dayPad = "0"
- else
- dayPad = ""
- end if
-
- returnVal = monthPad & Month(myDate) & DATEDELIM & dayPad & Day(myDate) & DATEDELIM & Year(myDate)
- end if
-
- formatDate = returnVal
-
- end function ' END function formatDate
- </SCRIPT>
-
-
- <SCRIPT RUNAT=SERVER LANGUAGE=JSCRIPT>
- function write(txt)
- {
- // Write the string out to the browser as html
- Response.Write(txt);
- } //End write
-
- function debug(txt)
- {
- // Write the string out to the browser as html - include an indication that this is a debug statement
- var pre = "DEBUG: ";
- Response.Write(pre + txt + "<br>\n");
- } //End write
-
-
-
- //
- // Wrap an argument it the proper SQL delimiters based on it's data type and the database type
- //
- function wrapDelim(databaseType, dataType, fieldValue) {
-
- var returnVal = "";
- var datestr;
-
- // Check for null parameter values
- databaseType = (databaseType == null) ? "" : databaseType;
-
- // Temp Removed for good reason but kept for safe keeping
- //ODBCDatabaseType = (ODBCDatabaseType == null) ? "" : ODBCDatabaseType;
- //if (Application("databaseType") != null) {
- // databaseType = Application("databaseType");
- //}
-
- // Convert to all upper case for uniform comparisons
- databaseType = databaseType.toUpperCase();
- dataType = dataType.toLowerCase();
-
- if (fieldValue != null) {
- // Strings
- if (dataType == "string"){
- returnVal = "'"+fixSingleQuotes(fieldValue)+"'";
- } // Numbers
- else if (dataType == "number" || dataType == "boolean"){
- returnVal = fieldValue;
- } // Dates
- else if (dataType == "date") {
- if (databaseType == "ODBC") {
- datestr = convertDate(databaseType, fieldValue);
- if ((datestr == "") || (datestr == null))
- returnVal = null;
- else
- returnVal = "{d '"+convertDate(databaseType, fieldValue)+"'}";
- } // end ODBC if
- else {
- datestr = convertDate(databaseType, fieldValue);
- if ((datestr == "") || (datestr == null))
- returnVal = null;
- else
- returnVal = "'"+datestr+"'";
- }
- } // end Date if
- else returnVal = "'"+fieldValue+"'";
- } // end fieldValue if
- else {
- returnVal = "";
- }
-
- return returnVal;
-
- } // END function wrapDelim
-
-
- //
- // Convert JavaScript date string literal to a date string literal acceptable to the database
- //
- /*
- DESCRIPTION:
- Reads in representation of a Date object as a string,
- e.g. "Mon Jan 23 03:30:15 1994", and converts it to a string
- literal format for a date literal demanded by a specified database type.
-
- NOTE: This function assumes the default date format for each database type is used.
- If this format is changed by the DBA or system administrator, this function may
- fail to format dates properly in the SQL statement.
- */
- function convertDate(databaseType, myDateStr) {
-
- var myDate = new Object();
- var returnVal = "";
-
- // Undo formatting of date string and place date in new date object
- myDate = undoFormatDate(myDateStr);
-
- if (myDate != null) {
- // Get date pieces for database specific formatting
- fmonth = ((myDate.getMonth() + 1) + "").length == 1 ? "0" + (myDate.getMonth() + 1) : myDate.getMonth() + 1;
- fmonthString = getMonthStr(fmonth);
- fdate = (myDate.getDate() + "").length == 1 ? "0" + myDate.getDate() : myDate.getDate();
- fyear = myDate.getYear() + 1900;
-
- // Create a new date string literal depending on the database type
- if (databaseType.toUpperCase() == "INFORMIX")
- returnVal = fmonth + "/" + fdate + "/" + fyear; // mm/dd/yyyy
- else if (databaseType.toUpperCase() == "ORACLE")
- returnVal = fmonthString + " " + fdate + ", " + fyear; // mmm dd, yyyy
- else if (databaseType.toUpperCase() == "ODBC")
- returnVal = fyear+"-"+fmonth+"-"+fdate; // yyyy-mm-dd
- else
- returnVal = fyear+"-"+fmonth+"-"+fdate; // ANSI SQL standard format, yyyy-mm-dd
- }
-
- return returnVal;
- } // END function convertDate
-
-
- //
- // Parses out a date string in the format mm-dd-yyyy into a date object
- //
- function undoFormatDate ( dateStr ) {
-
- var month = date = year = 0;
- var tempStr = "";
- var theDate = null;
-
- if ((dateStr != null) && (dateStr != "")) {
-
- // initialize counter
- var count = 1;
- // The loop cycles through the date string. It builds up numbers in tempStr until
- // it finds a character in dateStr. At that time it parses tempStr into a numeric
- // value and depending on it's position in the string (determined by "count") it
- // is assigned to the month, date or year.
- for (var j=0; j <= dateStr.length; j++) {
- // Short-circuit evaluation
- if ((j != dateStr.length) && (IsNum(dateStr.charAt(j)))) {
- tempStr += dateStr.charAt(j);
- }
- else {
- if (count == 1) {
- // If count == 1, we're at the first set of numbers ==> the month in mm-dd-yyyy
- month = parseInt(tempStr, 10) - 1; // subtract 1 to adjust for what Date constructor needs
- tempStr = "";
- count++;
- } else if (count == 2) {
- // If count == 2, we're at the first set of numbers ==> the date in mm-dd-yyyy
- date = parseInt(tempStr, 10);
- tempStr = "";
- count++;
- } else if (count == 3) {
- // If count == 3, we're at the first set of numbers ==> the year in mm-dd-yyyy
- year = parseInt(tempStr, 10);
- // Fix for one of JavaScript's Date bugs - if year >= 2000, must feed in
- // year - 1900, not just the year as you can do with years < 2000.
- // Note: this bug is not relavant in JScript
- //if (year >= 2000)
- //year = year - 1900;
- tempStr = "";
- count++;
- } // end if
-
- } // end else
- } // end for
-
- // Create date object with parsed values
- theDate = new Date(year, month, date);
-
- } // end if
-
- return theDate;
- } // END undoFormatDate
-
-
- //
- // Formats a value before it's added to a URL.
- // Also handles any special formatting for certain data types, e.g. dates
- //
- function formatDataValue (value, dataType) {
-
- var formattedVal = value;
-
- // Handle special cases for certain data types.
- // Dates will always be formatted according to the "formatDate()" function
- // No other data types require special handling at this time.
- if (dataType.toUpperCase() == "DATE") {
- formattedVal = formatDate(value);
- }
-
- return formattedVal;
- } // END formatDataValue
-
-
- //
- // Translate numeric month representation into 3-character month string
- //
- function getMonthStr(month)
- {
- var returnVal = "";
-
- // Check for null and exit if true
- if (month == null)
- return "";
-
- if (month == 1){
- returnVal = "Jan"
- }
- else if (month == 2){
- returnVal = "Feb"
- }
- else if (month == 3){
- returnVal = "Mar"
- }
- else if (month == 4){
- returnVal = "Apr"
- }
- else if (month == 5){
- returnVal = "May"
- }
- else if (month == 6){
- returnVal = "Jun"
- }
- else if (month == 7){
- returnVal = "Jul"
- }
- else if (month == 8){
- returnVal = "Aug"
- }
- else if (month == 9){
- returnVal = "Sep"
- }
- else if (month == 10){
- returnVal = "Oct"
- }
- else if (month == 11){
- returnVal = "Nov"
- }
- else if (month == 12){
- returnVal = "Dec"
- }
- else returnVal = "";
-
- return returnVal;
- } // END function getMonthStr
-
-
-
- //////////////////////////////////
- // //
- // Validation Functions //
- // //
- //////////////////////////////////
-
- //
- // Functions for testing the validity of various strings and numeric values
- //
-
-
- /*=============================================================
-
- FUNCTION: IsAlpha
-
- PURPOSE: Determines if a given string contains only alphabetic characters.
-
- INPUT: str (string) - the string to be tested
-
- RETURNS: (Boolean) - true, if the string contains only alphabetic characters (of any case)
- false, otherwise.
-
- DESCRIPTION: This function simply looks through a string to see if it contains only alphabetic
- characters, of upper or lower case.
-
- =============================================================*/
-
- function IsAlpha( str )
- {
- var isValid = true;
-
- str += ""; // convert to a string for performing string comparisons.
-
- // Loop through string one character at time, breaking out of for
- // loop when an non Alpha character is found.
-
- for (i = 0; i < str.length; i++)
- {
- // Alpha must be between "A"-"Z", or "a"-"z"
- if ( !( ((str.charAt(i) >= "a") && (str.charAt(i) <= "z")) ||
- ((str.charAt(i) >= "A") && (str.charAt(i) <= "Z")) ) )
- {
- isValid = false;
- break;
- }
- } // END for
-
- return isValid;
-
- } // END FUNCTION IsAlpha
-
-
-
- /* ===================================================
- FUNCTION: IsAlphaNum
-
- INPUT: numstr - a string that will be tested to ensure that
- each character is a digit or a letter.
-
- RETURN: true, if all characters in the string are a character from 0-9
- or a-z otherwise the function returns false
-
- NOTE: This function is used to test strings only. Inputting
- a numeric or boolean variable will give unpredictable results.
-
- =================================================== */
-
- function IsAlphaNum( str )
- {
- var i = 0;
- var isValid = false;
-
- // convert to a string for performing string comparisons.
- str += "";
-
-
- // Loop through length of string and test for any alpha numeric
- // characters
- for (i = 0; i < str.length; i++)
- {
- // Alphanumeric must be between "0"-"9", "A"-"Z", or "a"-"z"
- if ( ((str.charAt(i) >= "0") && (str.charAt(i) <= "9")) ||
- ((str.charAt(i) >= "a") && (str.charAt(i) <= "z")) ||
- ((str.charAt(i) >= "A") && (str.charAt(i) <= "Z")) )
- isValid = true;
- } // END for
-
- return isValid;
-
- } // END FUNCTION IsAlphaNum
-
-
-
- /*=============================================================
-
- FUNCTION: IsAlphaNumOrUnderscore
-
- PURPOSE: Determines if a given string contains only alphanumeric characters or underscores.
-
- INPUT: str (string) - the string to be tested
-
- RETURNS: (Boolean) - true, if the string contains only alphanumeric characters or underscores.
- false, otherwise.
-
- DESCRIPTION: This function simply looks through a string to see if it contains only alphanumeric
- characters or underscore characters.
-
- =============================================================*/
-
- function IsAlphaNumOrUnderscore( str )
- {
-
- var i = 0;
- var isValid = true;
-
- str += ""; // convert to a string for performing string comparisons.
-
- // Loop through string one character at a time. If non-alpha numeric
- // is found then, break out of loop and return a false result
-
- for (i = 0; i < str.length; i++)
- {
- // Alphanumeric must be between "0"-"9", "A"-"Z", or "a"-"z"
- if ( !( ((str.charAt(i) >= "0") && (str.charAt(i) <= "9")) ||
- ((str.charAt(i) >= "a") && (str.charAt(i) <= "z")) ||
- ((str.charAt(i) >= "A") && (str.charAt(i) <= "Z")) ||
- (str.charAt(i) == "_") ) )
- {
- isValid = false;
- break;
- }
-
- } // END for
-
- return isValid;
-
- } // END FUNCTION IsAlphaNumOrUnderscore
-
-
-
- /* ===================================================
- FUNCTION: IsBlank
-
- INPUT: teststr - the string to be tested
-
- RETURN: true, if the string is null or is an empty string, ""
- false, otherwise.
-
- NOTE: This function is used to test strings only. Entering
- a numeric or Boolean variable will give unpredictable results.
-
- =================================================== */
-
- function IsBlank( teststring )
- {
- var result = false;
-
- if ( (teststring == null) || (teststring == "") )
- result = true;
-
- return result;
-
- } // END FUNCTION IsBlank
-
-
-
- /* ===================================================
- FUNCTION: IsNum
-
- INPUT: numstr - a string that will be tested to ensure that
- each character is a digit
-
- RETURN: true, if all characters in the string are a character from 0-9
- false, otherwise.
-
- NOTE: This function is used to test strings only. Inputting
- a numeric or boolean variable will give unpredictable results.
- =================================================== */
-
- function IsNum( numstr )
- {
- var IsValid = true;
-
- // convert to a string for performing string comparisons.
- numstr += "";
-
- // Loop through string and test each character. If any
- // character is not a number, return a false result.
-
- for (i = 0; i < numstr.length; i++)
- {
- if (! ((numstr.charAt(i) >= "0") &&
- (numstr.charAt(i) <= "9")) ) {
- IsValid = false;
- break;
- }
-
- } // END for
-
- return IsValid;
-
- } // END FUNCTION IsNum
-
-
-
- //////////////////////////////////////////////////
- // //
- // Cursor Count Functions //
- // //
- //////////////////////////////////////////////////
-
- /* The following two functions are used to determine when to close cursors and possibly
- * database connections on a page. Cursors and connections must be closed at the end
- * of use for ASP 3.0.
- * The functions will increment or decrement a counter value that only exists per Session.
- * The counter will be incremented in all of the constructors for objects that use cursors,
- * and will be decremented in the render methods of the same objects. Each render method will
- * check the value after decrementing to see if it has reached zero. If so, that means all
- * cursor-using components on the page have fired and the cursor may now be safely closed.
- */
-
-
- //
- // incCursorCallCount increments the global page cursor usage counter
- //
- function incCursorCallCount() {
-
- // If not called for first time, increment
- if ((Session("amaspHidden_CursorCallCount") != null) && (Session("amaspHidden_CursorCallCount") != "undefined"))
- Session("amaspHidden_CursorCallCount") = (parseInt(Session("amaspHidden_CursorCallCount"), 10) + 1) + "";
- else
- Session("amaspHidden_CursorCallCount") = 1 + ""; // if called for the first time, init. to 1
-
- return parseInt(Session("amaspHidden_CursorCallCount"), 10);
-
- } // END incCursorCallCount
-
-
- //
- // decCursorCallCount decrements the global page cursor usage counter
- //
- function decCursorCallCount(queryComponentName) {
-
- var callCount;
- Session("amaspHidden_CursorCallCount") = (parseInt(Session("amaspHidden_CursorCallCount"), 10) - 1) + "";
-
- callCount = parseInt(Session("amaspHidden_CursorCallCount"), 10);
-
- // If we've reached the bottom component, close it's cursor component.
- if (callCount == 0) {
- eval(queryComponentName + ".closeCursor()");
- // Also, if not using a global connection, disconnect from DB
- if (Application("globalConn") != "true");
- //Temp close connection;
- } // end outer if
-
- return
-
- } // END decCursorCallCount
-
-
-
- //////////////////////////////////////////////////
- // //
- // Custom JavaScript Object: NewRequest //
- // //
- //////////////////////////////////////////////////
-
- function NewRequest () {
-
- // Constant Properties
-
- // All properties passed via the request object which
- // are generated by these components contain this special
- // prefix in their property name.
- this.UNIQUEPREFIX = "amasp";
-
- // Properties that indicate a field name, such as from a DBDynaField or a keyfield from a DBList,
- // contain this prefix. This allows the NewRequest object to strip off the prefix so the DBQuery
- // object knows what fields to add to it's 'where' clause.
- this.FIELDPREFIX = this.UNIQUEPREFIX + "Field_";
-
- // Properties
- this.oldRequest = new Object();
- this.newRequest = new Object();
-
- // Methods
- this.parseOldRequest = nrParseOldRequest;
- this.smartClone = nrSmartClone;
-
- // Initialize object:
-
- // JJK Bug Fix, 980603 BEGIN
- var rform = "";
- var rqstring = "";
-
- // Make sure no undefined value exist. If they do, just set to empty string.
- if (Request.Form+"" != "undefined")
- rform = Request.Form+"";
- if (Request.QueryString+"" != "undefined")
- rqstring = Request.QueryString+"";
-
- // Parse out values from request and put in oldRequest property
- this.parseOldRequest(rform + rqstring, 0);
- // JJK, 980603 END
-
- // Clone old Request object and use intelligent processing to strip out unwanted values
- this.newRequest = this.smartClone( this.oldRequest );
-
- } // END NewRequest constructor
-
-
- function nrParseOldRequest ( mystr, lastIndex ) {
-
- var ampIdx = 0; // index of the right-most '&' char in the string
- // var rObj = new Object(); // initialize variable as a blank object
-
- ampIdx = mystr.indexOf("&", lastIndex);
-
- // Loop recursively until we reach the end of the string (signified by no-more &'s)
- if (ampIdx != -1)
- this.parseOldRequest(mystr, ampIdx + 1 );
-
- // Strip off any unprocessed name=value pairs on the left side of the string
- mystr = mystr.substring(lastIndex, mystr.length);
-
- // Find the divider between the name and the value
- tHalfIdx = mystr.indexOf("=");
- // Find the end of the left-most name=value pair
- tEndIdx = mystr.indexOf("&") != -1 ? mystr.indexOf("&") : mystr.length;
-
- // Store name=value pair as object.name=URLDecode(value) i.e. URLDecode value first.
- this.oldRequest[mystr.substring(0, tHalfIdx)] = URLDecode(mystr.substring(tHalfIdx+1, tEndIdx));
-
- } // END nrParseOldRequest
-
-
- //
- // Clone only the properties of the request object that are not default properties (e.g. request.ip)
- // This does allow for a custom user-defined property that has the same name as a default property, however.
- //
- function nrSmartClone ( obj ) {
-
- var temp = "";
- var flength = 0;
- var plength = 0;
-
- // Assign length to a variable. Workaround for JScript bug and substring function.
- // substring will not accept "mystring.length" for both of it's length fields. Always
- // returns an empty string.
- flength += this.FIELDPREFIX.length;
-
- // Loop through request object and add only properties prefixed with this.UNIQUEPREFIX
- // into the new request object to ensure no built-in LiveWire properties are included
- // with the new request object properties.
- for (var prop in obj) {
- temp = prop + "";
-
- if (prop.indexOf(this.UNIQUEPREFIX) != -1) {
- if (prop.indexOf(this.FIELDPREFIX) != -1) {
- // Workaround for JScript bug in substring function
- plength = prop.length;
- // Strip off the prefix indicated by this.FIELDPREFIX
- temp = prop.substring(flength, plength);
- }
- // Create and populate the properties of the newRequest object
- this.newRequest[temp] = obj[prop];
-
- }
-
- } // end for
-
- return this.newRequest;
-
- } // END nrSmartClone
-
-
- //
- // Remove any plus signs that are introduced as part of URL encoding
- // Then unescape the string and return it.
- //
- function URLDecode ( mystr ) {
-
- var newstr = "";
-
- // Loop through each char in string.
- // If it's a +, replace with a space in the new string
- // else, just insert the same character as in mystr
- for (var i = 0; i < mystr.length; i++) {
- if (mystr.charAt(i) == "+")
- newstr += " ";
- else
- newstr += mystr.charAt(i);
- }
-
- return unescape(newstr);
-
- } // END URLDecode
-
-
- //
- // Convert empty strings to nulls for input of specific data types
- //
- function convertForInput(dataType, fieldValue) {
-
- if ((dataType == "number" || dataType == "boolean" || dataType == "date") && (fieldValue == null || fieldValue == "null" || fieldValue == "")) {
- return null;
- }
- else {
- return fieldValue;
- }
-
- } // end convertForInput
-
- function viewObject (obj,objName) {
-
- write("\n<BR><B>"+objName+" Properties: </B><HR>");
- for (prop in obj) {
- write("\n<BR>"+objName+"."+prop+" = "+obj[prop]);
- }
- write("\n<BR><HR><BR>");
- }
-
- function fixSingleQuotes(inputstring) {
- // fixSingleQuotes("'Test1Tes't1Test1''Test1'")
- var newstring = "";
-
- for (var i = 0; i < inputstring.length; i++) {
- newstring += (inputstring.charAt(i) == "'") ? inputstring.charAt(i) +"'" : inputstring.charAt(i) +"";
- }
-
- return newstring;
- }
-
- </SCRIPT>